package email.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.swing.JOptionPane;
import javax.swing.JTable;

import email.model.Email;
import email.model.MailServer;
import email.view.MailTable;


/**
 * Class for downloading the Mails from the Server, and to load it into the Mail Table.
 * @author Dominik Roth
 *
 */


public class RefreshMailTableListener extends Thread implements ActionListener {

	private static JTable table;
	private String folder;
	private static ArrayList<Email> mails;

	/**
	 * Constructor for the table
	 * @param table
	 * @param folder
	 */
	public RefreshMailTableListener(JTable table, String folder)
	{
		this.table = table;
	}
	
	
	/**
	 * Refreshes the Table
	 */
	public void run()
	{
		Mailsabrufen();
		table.setModel(new MailTable(mails));
		table.repaint();
	}
	
	public void actionPerformed(ActionEvent arg0) 
	{
		Mailsabrufen();
		table.setModel(new MailTable(mails));
		table.repaint();
	}
		
	/**
	 * Connect to the Mailserver and get the Mails
	 * @return
	 */
	public static ArrayList<Email> Mailsabrufen(){
		mails  = new ArrayList<Email>();

		Properties props = new Properties();
		props.setProperty("mail.store.protocol", "imaps");
		try {
			Session session = Session.getInstance(props, null);
			Store store = session.getStore();
			store.connect(MailServer.getInstance().getImapUrl(), MailServer.getInstance().getUsername(), new String(MailServer.getInstance().getPassword()));
			Folder inbox = store.getFolder("INBOX");
			inbox.open(Folder.READ_ONLY);
			int maxDownloadCount = 0;
			if(MailServer.getInstance().getReadMailCount()!=0)
				maxDownloadCount = inbox.getMessageCount() - MailServer.getInstance().getReadMailCount();
			for(int i=inbox.getMessageCount();i>maxDownloadCount;i--)
			{
				Message msg = inbox.getMessage(i);
				Address[] in = msg.getFrom();
				Address[] to = msg.getRecipients(RecipientType.TO);
				Address[] cc = msg.getRecipients(RecipientType.CC);
				Address[] bcc = msg.getRecipients(RecipientType.BCC);

			
				Email mail = new Email();
				mail.setSubject(msg.getSubject());
				
				Object content = msg.getContent(); 
				String mailText = "";
				if (content instanceof String)  
				{  
				    mailText = (String)content;  
				    
				}  
				else if (content instanceof Multipart)  
				{  
				    Multipart multipart = (Multipart)content;
				    for(int j = 0;j<multipart.getCount();j++)
				    {
				    	mailText += multipart.getBodyPart(j).getContent();
				    }
				}
				
				mail.setMailtext(mailText);
				
				for(Address address : in) {
					mail.setSender(address);
				}
				for(Address address : to) {
					mail.getReceiver().add(address);
				}
				mail.setDate(msg.getReceivedDate());
				mails.add(mail);
				
			}

		} catch (MessagingException authenti){
			JOptionPane.showMessageDialog(null,"Wrong password","Password", JOptionPane.ERROR_MESSAGE);
			return null;
		}
		  catch (Exception mex) {
			mex.printStackTrace();
		}
		return mails;


	}
}
